﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.UI;

namespace dsconvert
{
    public class SqlHierarchyData : IHierarchyData, ICustomTypeDescriptor
    {
        private DataRowView item;
        private string dataParentIdField;
        private string dataIdField;

        public SqlHierarchyData(string dataParentIdField, string dataIdField, DataRowView item)
        {
            this.item = item;
            this.dataParentIdField = dataParentIdField;
            this.dataIdField = dataIdField;
        }

        bool IHierarchyData.HasChildren
        {
            get
            {
                foreach (DataRowView row in item.DataView)
                {
                    object[] array1 = row.Row.ItemArray;
                    object[] array2 = item.Row.ItemArray;
                    string a = row[dataParentIdField].ToString();
                    string b = item[dataIdField].ToString();
                    string c = item[dataParentIdField].ToString();

                    //    HttpContext.Current.Response.Write(array1[2] + " - " + a + " -- " + array2[2] + " - " + b + " <br> "); 
                    /*if( (int.Parse(a)>10))
                        return false;*/
                    if (a == b)
                    {
                        return true;
                    }
                }

                //  return  true;
                return false;
            }
        }


        public Int32 Value
        {
            get;
            set;
        }

        public Int32 ParentID
        {
            get;
            set;
        }

        public String Text
        {
            get;
            set;
        }
        object IHierarchyData.Item
        {
            get { return item; }
        }

        public override string ToString()
        {
            return dataIdField;
        }

        string IHierarchyData.Path
        {
            get
            {
                /*string path = "/*[position()=1]";

                GetPath(item, ref path);
                return path;*/
                return null;
            }
        }

        private void GetPath(DataRowView crow, ref string path)
        {

            foreach (DataRowView row in item.DataView)
            {
                string c, d;
                c = crow.Row.ItemArray[2].ToString();
                d = crow.Row.ItemArray[0].ToString();
                string a = crow[dataParentIdField].ToString();
                string b = row[dataIdField].ToString();
                if (a == b)
                {
                    path = "/*[position()=1]" + path;

                    //GetPath(row, ref path);
                    //HttpContext.Current.Response.Write("begin<br>" + c + "--" + a + "--" + b + "<br>end<br>");
                    //HttpContext.Current.Response.Write(path + "<br>");
                }
                //HttpContext.Current.Response.Write(c + "--" + a + "--" +b + "<br>");

            }
        }

        string IHierarchyData.Type
        {
            get { return dataIdField; }
        }

        //获取子节点
        IHierarchicalEnumerable IHierarchyData.GetChildren()
        {
            SqlHierarchicalEnumerable children = new SqlHierarchicalEnumerable();

            foreach (DataRowView row in item.DataView)
            {
                string a = row[dataParentIdField].ToString();
                string b = item[dataIdField].ToString();
                if (a == b)
                    children.Add(new SqlHierarchyData(dataParentIdField, dataIdField, row));
            }

            return children;
        }

        //获取父节点
        IHierarchyData IHierarchyData.GetParent()
        {
            foreach (DataRowView row in item.DataView)
            {
                string a = item[dataParentIdField].ToString();
                string b = row[dataIdField].ToString();
                if (a == b)
                    return new SqlHierarchyData(dataParentIdField, dataIdField, row);
            }

            return null;
        }

        #region ICustomTypeDescriptor Members

        System.ComponentModel.AttributeCollection ICustomTypeDescriptor.GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        string ICustomTypeDescriptor.GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        string ICustomTypeDescriptor.GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        TypeConverter ICustomTypeDescriptor.GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection pds = TypeDescriptor.GetProperties(item);

            if (pds.Count > 0)
            {
                List<SqlHierarchyDataPropertyDescriptor> list = new List<SqlHierarchyDataPropertyDescriptor>();

                foreach (PropertyDescriptor pd in pds)
                {
                    list.Add(new SqlHierarchyDataPropertyDescriptor(pd.Name));
                }

                SqlHierarchyDataPropertyDescriptor[] arr = new SqlHierarchyDataPropertyDescriptor[list.Count];
                list.CopyTo(arr);

                return new PropertyDescriptorCollection(arr);
            }


            return PropertyDescriptorCollection.Empty;

        }

        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
        {
            return ((ICustomTypeDescriptor)this).GetProperties(null);
        }

        object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
        {
            if (pd is SqlHierarchyDataPropertyDescriptor)
            {
                return this;
            }

            return null;
        }

        #endregion
    }

    public class SqlHierarchyDataPropertyDescriptor : PropertyDescriptor
    {
        private string name;


        public SqlHierarchyDataPropertyDescriptor(string name)
            : base(name, null)
        {
            this.name = name;
        }

        public override string Name
        {
            get { return name; }
        }

        public override Type ComponentType
        {
            get { return typeof(SqlHierarchyData); }
        }

        public override bool IsReadOnly
        {
            get { return true; }
        }

        public override Type PropertyType
        {
            get { return Type.GetType("System.String"); }
        }


        public override bool CanResetValue(object o)
        {
            return false;
        }

        public override object GetValue(object o)
        {
            SqlHierarchyData shd = o as SqlHierarchyData;

            if (shd != null)
            {
                IHierarchyData hd = (IHierarchyData)shd;
                string subject = ((DataRowView)(hd.Item))[name].ToString();
                return subject;
            }

            return null;
        }

        public override void ResetValue(object o)
        {
            throw new NotSupportedException();
        }


        public override void SetValue(object o, object value)
        {
            throw new NotSupportedException();
        }

        public override bool ShouldSerializeValue(object o)
        {
            return true;
        }

        public override TypeConverter Converter
        {
            get { return TypeDescriptor.GetConverter(typeof(System.String)); }
        }
    }
}